package com.quectel.core.module.partyeducationexam.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.core.partyeducationexam.PartyEducationQuestionConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.partybuilding.dto.PartyOrganizationDto;
import com.quectel.core.module.partybuilding.service.PartyOrganizationService;
import com.quectel.core.module.partyeducationexam.dao.PartyEducationQuestionDao;
import com.quectel.core.module.partyeducationexam.dto.PartyEducationQuestionBankDto;
import com.quectel.core.module.partyeducationexam.dto.PartyEducationQuestionDto;
import com.quectel.core.module.partyeducationexam.entity.PartyEducationQuestionEntity;
import com.quectel.core.module.partyeducationexam.service.PartyEducationQuestionBankService;
import com.quectel.core.module.partyeducationexam.service.PartyEducationQuestionService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/06/16 15:12
 */
@DubboService
public class PartyEducationQuestionServiceImpl extends ServiceImpl<PartyEducationQuestionDao, PartyEducationQuestionEntity> implements PartyEducationQuestionService{

    private static final Logger LOGGER = LoggerFactory.getLogger(PartyEducationQuestionServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> PARTY_EDUCATION_QUESTION_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "PARTY_EDUCATION_QUESTION_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<PartyEducationQuestionDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(PARTY_EDUCATION_QUESTION_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private PartyEducationQuestionDao partyEducationQuestionDao;
    @Autowired
    private PartyOrganizationService partyOrganizationService;
    @Autowired
    private PartyEducationQuestionBankService partyEducationQuestionBankService;


    @Override
    public void saveBatch(List<PartyEducationQuestionDto> list) {
        List<PartyEducationQuestionEntity> partyEducationQuestionEntities = CopyUtils.copyList(list, PartyEducationQuestionEntity.class);
        for (PartyEducationQuestionEntity partyEducationQuestionEntity : partyEducationQuestionEntities) {
            partyEducationQuestionEntity.setId(Snowflake.nextId());
        }
        super.saveBatch(partyEducationQuestionEntities);
    }

    @Override
    public int queryTotalByBankId(Long bankId) {
        Long aLong = partyEducationQuestionDao.selectCount(new LambdaQueryWrapper<PartyEducationQuestionEntity>()
                .eq(PartyEducationQuestionEntity::getIsDel, PartyEducationQuestionConstants.IsDelEnum.NO.getStatus())
                .eq(PartyEducationQuestionEntity::getBankId, bankId));
        return aLong.intValue();
    }

    @Override
    public PartyEducationQuestionDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PARTY_EDUCATION_QUESTION_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public PartyEducationQuestionDto selectById(Long id){
            PartyEducationQuestionEntity entity = partyEducationQuestionDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,PartyEducationQuestionDto.class));
    }

    @Override
    public List<PartyEducationQuestionDto> queryList(Map<String,Object> params){
        List<PartyEducationQuestionEntity> list = partyEducationQuestionDao.queryList(params);
        List<PartyEducationQuestionDto> result =  CopyUtils.copyList(list,PartyEducationQuestionDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<PartyEducationQuestionDto> queryListWithoutPadding(Map<String,Object> params){
        List<PartyEducationQuestionEntity> list = partyEducationQuestionDao.queryList(params);
        return CopyUtils.copyList(list,PartyEducationQuestionDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private PartyEducationQuestionDto paddingField(PartyEducationQuestionDto dto){
        if (dto != null){
            PartyOrganizationDto partyOrganizationDto = partyOrganizationService.selectCacheById(dto.getPartyId());
            dto.setParty(partyOrganizationDto);

            PartyEducationQuestionBankDto partyEducationQuestionBankDto = partyEducationQuestionBankService.selectCacheById(dto.getBankId());
            dto.setQuestionBank(partyEducationQuestionBankDto);
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return partyEducationQuestionDao.queryTotal(params);
    }

    @Override
    public Long save(PartyEducationQuestionDto dto){
        PartyEducationQuestionEntity entity = CopyUtils.copyObj(dto,PartyEducationQuestionEntity.class);
        entity.setId(Snowflake.nextId());
        partyEducationQuestionDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(PartyEducationQuestionDto dto){
        PartyEducationQuestionEntity entity = CopyUtils.copyObj(dto,PartyEducationQuestionEntity.class);

        partyEducationQuestionDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(PartyEducationQuestionDto dto) {
       PartyEducationQuestionEntity entity = CopyUtils.copyObj(dto,PartyEducationQuestionEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, partyEducationQuestionDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

//        partyEducationQuestionDao.deleteById(id);
        PartyEducationQuestionDto updateObj = new PartyEducationQuestionDto();
        updateObj.setId(id);
        updateObj.setIsDel(PartyEducationQuestionConstants.IsDelEnum.YES.getStatus());
        updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
